package com.video.dao.http;

import okhttp3.*;

import java.io.IOException;

public class ResembleAITextToSpeech {
    private static final String API_KEY = "your_api_key"; // 替换为你的 Resemble AI API 密钥
    private static final String PROJECT_UUID = "your_project_uuid"; // 替换为你的项目 UUID
    private static final String VOICE_UUID = "your_voice_uuid"; // 替换为你的语音 UUID
    private static final String RESEMBLE_API_URL = "https://app.resemble.ai/api/v1/projects/" + PROJECT_UUID + "/clips";

    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();

        // SSML 内容
        String ssmlText = "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'>"
                + "<voice name='" + VOICE_UUID + "'>"
                + "Hello, this is a test of Resemble AI with SSML. <break time='500ms'/> Ha ha ha!"
                + "</voice>"
                + "</speak>";

        // 请求体
        MediaType mediaType = MediaType.parse("application/json");
        String jsonBody = "{"
                + "\"body\": \"" + ssmlText + "\","
                + "\"voice\": \"" + VOICE_UUID + "\","
                + "\"title\": \"Java SSML Test\""
                + "}";
        RequestBody body = RequestBody.create(jsonBody, mediaType);

        // 创建请求
        Request request = new Request.Builder()
                .url(RESEMBLE_API_URL)
                .post(body)
                .addHeader("Authorization", "Bearer " + API_KEY)
                .addHeader("Content-Type", "application/json")
                .build();

        // 发送请求
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                // 解析响应
                String responseBody = response.body().string();
                System.out.println("Response: " + responseBody);

                // 提取音频文件 URL
                String audioUrl = responseBody.split("\"audio_src\":\"")[1].split("\"")[0];
                System.out.println("Audio URL: " + audioUrl);

                // 下载音频文件
                downloadAudioFile(client, audioUrl, "output.mp3");
            } else {
                System.err.println("Request failed: " + response.code() + " " + response.message());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 下载音频文件
    private static void downloadAudioFile(OkHttpClient client, String audioUrl, String outputFilePath) {
        Request request = new Request.Builder()
                .url(audioUrl)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                byte[] audioData = response.body().bytes();
                java.nio.file.Files.write(java.nio.file.Paths.get(outputFilePath), audioData);
                System.out.println("Audio file saved as " + outputFilePath);
            } else {
                System.err.println("Failed to download audio: " + response.code() + " " + response.message());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
